import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/12 10:56
 * @description 最优解法
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int[] mapping =  {8,9,4,0,2,1,3,5,7,6}, nums = {991,338,38};
        solution2.sortJumbled(mapping, nums);
    }
    public  int[] sortJumbled(int[] mapping, int[] nums) {
        int n = nums.length;
        long[] hash = new long[n];
        for (int i = 0; i < n; i++) {
            // 这个还构造了 i 在里面，首先转换后数值大，那么hash值大，数值相同，那么排在前面的hash值小，排在后面的hash值大
            hash[i] = (long) map(mapping, nums[i]) * (n + 1) + i;
        }
        Arrays.sort(hash);
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = nums[(int) (hash[i] % (n + 1))];
        }
        return res;
    }

    private int map(int[] mapping, int num) {
        if (num == 0) return mapping[0];
        int res = 0;
        int bit = 1;
        while (num > 0) {
            // 从低位到高位，模拟乘
            res += mapping[num % 10] * bit;
            bit *= 10;
            num /= 10;
        }
        return res;
    }
}
